/**
 * 
 */
package com.hson.demo.base.web.clickstream;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.hson.demo.base.util.HttpUtils;


/**
 * @author Hungel zou
 * @time 2014年10月10日 下午5:27:17
 */
@SuppressWarnings("rawtypes")
public class ClickstreamRequest implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4403852799481041177L;

	private String protocol;
	private String serverName;
	private int serverPort;
	private String requestURI;
	private String queryString;
	private String remoteUser;
	private Date timestamp;
	private Map paramMap;
	private Long userId;
	private String visitIp;
	private String sessionId;
	private String referer;
	private String userAgent;

	public ClickstreamRequest(HttpServletRequest request, Date timestamp) {
		protocol = request.getProtocol();
		serverName = request.getServerName();
		serverPort = request.getServerPort();
		requestURI = request.getRequestURI();
		String qs = request.getQueryString();
		if (StringUtils.isNotEmpty(qs))
			try {
				qs = URLDecoder.decode(qs, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		queryString = qs;
		remoteUser = request.getRemoteUser();
		this.timestamp = timestamp;
		paramMap = request.getParameterMap();
		visitIp = HttpUtils.getRemoteAddr(request);
		sessionId = request.getSession().getId();
		referer = request.getHeader("referer");
		userAgent = request.getHeader("User-Agent");
		if (request.getSession().getAttribute("userId") != null) {
			try {
				userId = (Long) request.getSession().getAttribute("userId");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @return the protocol
	 */
	public String getProtocol() {
		return protocol;
	}

	/**
	 * @return the serverName
	 */
	public String getServerName() {
		return serverName;
	}

	/**
	 * @return the serverPort
	 */
	public int getServerPort() {
		return serverPort;
	}

	/**
	 * @return the requestURI
	 */
	public String getRequestURI() {
		return requestURI;
	}

	/**
	 * @return the queryString
	 */
	public String getQueryString() {
		return queryString;
	}

	/**
	 * @return the remoteUser
	 */
	public String getRemoteUser() {
		return remoteUser;
	}

	/**
	 * @return the timestamp
	 */
	public Date getTimestamp() {
		return timestamp;
	}

	/**
	 * @return the paramMap
	 */
	public Map getParamMap() {
		return paramMap;
	}

	/**
	 * @return the userId
	 */
	public Long getUserId() {
		return userId;
	}

	/**
	 * @return the visitIp
	 */
	public String getVisitIp() {
		return visitIp;
	}

	/**
	 * @return the sessionId
	 */
	public String getSessionId() {
		return sessionId;
	}

	/**
	 * @return the referer
	 */
	public String getReferer() {
		return referer;
	}

	/**
	 * @return the userAgent
	 */
	public String getUserAgent() {
		return userAgent;
	}

	 /** 
     * Returns a string representation of the HTTP request being tracked. 
     * Example: <b>www.opensymphony.com/some/path.jsp?arg1=foo&arg2=bar</b> 
     * 
     * @return  a string representation of the HTTP request being tracked. 
     */  
	@Override
    public String toString() {  
        return serverName +  
                (serverPort != 80 ? ":" + serverPort : "") +  
                requestURI +  
                (queryString != null ? "?" + queryString : "");  
    }
}
